/*
 * @FilePath: /flutter_cart/lib/service/service_method.dart
 * @Author: Yongchao Wang
 * @Date: 2020-03-26 06:31:53
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-04-06 21:40:41
 */
import 'package:dio/dio.dart';
import 'dart:async';
import '../config/service_url.dart';

//获取首页基础内容
Future getHomePageContent() async {
  try {
    Response response;
    Dio dio = new Dio();
    dio.options.contentType = 'application/x-www-form-urlencoded';
    var formData = {'lon': '115.02932', 'lat': '35.76189'};
    response = await dio.post(servicePath['homePageContent'], data: formData);
    if (response.statusCode == 200) {
      return response.data;
    } else {
      throw Exception('接口错误');
    }
  } catch (e) {
    return print('Error:====');
  }
}

//获取首页基础内容
Future getHomePageBelowntent(page) async {
  try {
    Response response;
    Dio dio = new Dio();
    dio.options.contentType = 'application/x-www-form-urlencoded';
    response = await dio
        .post(servicePath['homePageBelowContent'], data: {'page': page});
    if (response.statusCode == 200) {
      return response.data;
    } else {
      throw Exception('接口错误');
    }
  } catch (e) {
    return print('Error:====');
  }
}

//获取首页基础内容
Future getCategory() async {
  try {
    Response response;
    Dio dio = new Dio();
    dio.options.contentType = 'application/x-www-form-urlencoded';
    response = await dio.post(servicePath['getCategory']);
    if (response.statusCode == 200) {
      return response.data;
    } else {
      throw Exception('接口错误');
    }
  } catch (e) {
    return print('Error:====');
  }
}

Future request(path, method, params) async {
  try {
    Response response;
    Dio dio = new Dio();
    dio.options.contentType = 'application/x-www-form-urlencoded';
    if (method.toLowerCase() == 'get') {
      response = await dio.get(servicePath[path], queryParameters: params);
    } else {
      response = await dio.post(servicePath[path], data: params);
    }
    if (response.statusCode == 200) {
      return response.data;
    } else {
      throw Exception('接口错误');
    }
  } catch (e) {
    return print('Error:====');
  }
}
